<template>
	<view class="custom-label-page page">
		<view class="form-block block">
			<view class="form-label">标签（{{label.length}}/5）</view>
			<view class="form-cotent">
				<view @click="deleteLabel(index)" class="label-item" v-for="(item,index) in label" :key="index">
					<view class="label">{{item}}</view>
					<view class="iconfont icon-guanbi"></view>
				</view>
			</view>
			<view v-if="label.length<5" class="input">
				<input v-model.trim="value" maxlength="10" placeholder-class="input-placeholder" placeholder="请输入标签" />
				<view @click="confirm" class="btn cancel">确定</view>
			</view>
		</view>
		<view @click="save" class="btn normal">保存</view>
	</view>
</template>

<script>
	export default{
		data(){
			return{
				value:'',
				label:[],
			}
		},
		methods:{
			deleteLabel(index){
				this.label.splice(index,1);
			},
			save(){
				if(this.label.length==0){
					this.showToast('请至少添加一个标签');
					return;
				}
				uni.$emit('confirmLabel',this.label.join(' | '));
				this.showToast('保存成功',()=>{
					uni.navigateBack()
				})
			},
			confirm(){
				if(!this.value){
					this.showToast('请先输入内容');
					return;
				}
				this.label.push(this.value);
				this.value='';
			}
		},
		onLoad(opt) {
			this.label=opt.label?opt.label.split(' | '):[];
		}
	}
</script>

<style lang="scss" scoped>
	.custom-label-page{
		padding: 30rpx;
		.form-block {
			padding: 30rpx;
			background: #fff;
			border-radius: 14px;
		
			.form-label {
				width: 100%;
				display: flex;
				align-items: center;
			}
		
			.form-cotent {
				width: 100%;
				display: flex;
				flex-wrap: wrap;
				margin-top: 30rpx;
				display: flex;
				flex-wrap: wrap;
				.label-item{
					border-radius: 6px;
					padding: 6rpx 30rpx;
					padding-right: 10rpx;
					margin-right: 20rpx;
					margin-bottom: 20rpx;
					display: flex;
					align-items: center;
					justify-content: center;
					background: #E8F4E4;
					.iconfont{
						color: $theme-color;
					}
					.label{
						margin-right: 10rpx;
						color: $theme-color;
						font-size: 28rpx;
					}
				}
			}
			.input{
				display: flex;
				align-items: center;
				justify-content: center;
				input{
					width: calc(100% - 200rpx);
					height: 60rpx;
					padding: 0 20rpx;
					font-size: 28rpx;
					border: 1px solid #dddddd;
					border-radius: 20px;
				}
				.btn.cancel{
					margin-left: 20rpx;
					background: $theme-color;
					color: #fff;
					width: 100rpx;
					border-radius: 5px;
					height: 60rpx;
					font-size: 28rpx;
					
				}
			}
		}
		.btn.normal{
			margin: 30rpx auto;
		}
	}
</style>